package com.ssq.mobilehelper.aes;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ssq.mobilehelper.global.AppSettings;
import com.ssq.mobilehelper.global.MyApp;
import com.ssq.mobilehelper.R;
import com.ssq.mobilehelper.greendao.gen.EncryptBeanDao;
import java.util.ArrayList;
import java.util.List;

/***
 * 加密主界面
 */
public class EncryptActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private ListView mListEncrypt;
    private FloatingActionButton mAddEncrypt;
    private List<EncryptBean> list;
    private EncryAdapter adapter;
    private EncryptBeanDao encryptBeanDao;
    private   AlertDialog.Builder builder;
    private   AlertDialog password;
    private   String str = null;
    private  String psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_encrypt);
        initView();

        setTitle("加密");
        initListView();
        setPassword();
    }

    //设置密码
    private void setPassword() {
        SharedPreferences pref = getSharedPreferences("password",MODE_PRIVATE);
        psw = pref.getString("password","");//第二个参数为默认值

        password = new AlertDialog.Builder(this).create();
        View view = LayoutInflater.from(this).inflate(R.layout.layout_password,null);
        final EditText edit = (EditText) view.findViewById(R.id.editPassword);
        Button btn = (Button) view.findViewById(R.id.btnPassword);
        password.setView(view);
        password.show();
        password.setCancelable(false);

        if(TextUtils.isEmpty(psw)){
            edit.setHint("第一次使用请设置密码");
        }else {
            edit.setHint("输入密码进入");
        }



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = edit.getText().toString();
                if(TextUtils.isEmpty(str))
                    return;

                //TODO 保存密码
                if(TextUtils.isEmpty(psw)){

                    SharedPreferences pref = EncryptActivity.this.getSharedPreferences("password",MODE_PRIVATE);
                    SharedPreferences.Editor editor = pref.edit();
                    AppSettings.PASSWORD = str ;

                    try {
                        str = AESUtils.encrypt(str,str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    editor.putString("password",str);
                    editor.commit();

                    password.dismiss();


                }else {

                    //TODO 登录
                    try {
                        psw = AESUtils.decrypt(edit.getText().toString(),psw);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    if(psw.equals(edit.getText().toString())){
                        AppSettings.PASSWORD = edit.getText().toString();
                        password.dismiss();
                    }else {
                        Toast.makeText(EncryptActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    }

                }


            }
        });

    }

    @Override
    protected void onResume() {

        super.onResume();

        list = encryptBeanDao.loadAll();
        adapter.setAdapter(list);
        adapter.notifyDataSetChanged();


    }

    private void initListView() {

        list = new ArrayList<>();

        encryptBeanDao = MyApp.getInstances().getDaoSession().getEncryptBeanDao();
        list = encryptBeanDao.loadAll();
        adapter = new EncryAdapter(getApplicationContext(),list);
        mListEncrypt.setAdapter(adapter);
    }

    private void initView() {
        mListEncrypt = (ListView) findViewById(R.id.list_encrypt);
        mAddEncrypt = (FloatingActionButton) findViewById(R.id.addEncrypt);
        mAddEncrypt.setOnClickListener(this);
        mListEncrypt.setOnItemClickListener(this);
        mListEncrypt.setOnItemLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addEncrypt:
                startActivity(new Intent(EncryptActivity.this,AddEncryptActivity.class));
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(EncryptActivity.this,LookActivity.class);

        Gson gson = new Gson();
        String temp = gson.toJson(list.get(i));

        intent.putExtra("intent",temp);
        startActivity(intent);
    }

    /**
     * 删除一条记录
     * @param adapterView
     * @param view
     * @param i
     * @param l
     * @return
     */
    @Override
    public boolean onItemLongClick(final AdapterView<?> adapterView, View view, int i, long l) {


        builder = new AlertDialog.Builder(this);
        CharSequence delete [] = {"删除这条信息"};
        builder.setTitle("是否删除？");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int i) {
//
//                encryptBeanDao.delete(list.get(i));
//                list = encryptBeanDao.loadAll();
//                adapter.setAdapter(list);
//                adapter.notifyDataSetChanged();
//
//            }
//
//            });
        builder.setItems(delete, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                encryptBeanDao.delete(list.get(i));
                list = encryptBeanDao.loadAll();
                adapter.setAdapter(list);
                adapter.notifyDataSetChanged();
            }
        });
        builder.show();
        return true;
    }

}
